package com.assessment.config;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.assessment.entity.Adep;
import com.assessment.entity.Apeo;
import com.assessment.service.AdepService;
import com.assessment.service.ApeoService;
import com.assessment.service.impl.AdepServiceImpl;
import com.assessment.service.impl.ApeoServiceImpl;
import com.baomidou.mybatisplus.extension.service.IService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

import static com.assessment.utils.Constant.BATCH_COUNT;


// 有个很重要的点 DataListener 不能被spring管理，要每次读取excel都要new,然后里面用到spring可以构造方法传进去
public class DataListener<M> extends AnalysisEventListener<M> {
    private static final Logger log = LoggerFactory.getLogger(DataListener.class);
    List<M> list = new ArrayList<>();// 用来存储数据
    private final IService<M> service;
    private final AdepService adepService;
    private final ApeoService apeoService;
    /**
     * 如果使用了spring,请使用这个构造方法。每次创建Listener的时候需要把spring管理的类传进来
     * @param service
     */
    public DataListener(IService<M> service,AdepService adepService,ApeoService apeoService) {
        this.service = service;
        this.adepService = adepService;
        this.apeoService = apeoService;
    }
    public DataListener(IService<M> service,AdepService adepService) {
        this.service = service;
        this.adepService = adepService;
        this.apeoService =  new ApeoServiceImpl();
    }
    public DataListener(IService<M> service) {
        this.service = service;
        this.adepService = new AdepServiceImpl();
        this.apeoService = new ApeoServiceImpl();
    }

    /**
     * 这个每一条数据解析都会来调用
     *
     * @param data
     *            one row value. Is is same as {@link AnalysisContext#readRowHolder()}
     * @param context
     */

    @Override
    public void invoke(M data, AnalysisContext context) {
        log.info("解析到一条数据:{}", data.toString());
        boolean canSave = true;
        if( data instanceof Adep ){
            canSave = adepService.checkDep((Adep)data);
        }else if ( data instanceof Apeo ){
            canSave = apeoService.checkPeo((Apeo)data);
        }
        log.info("处理之后:{}", data.toString());
        if (canSave)
            list.add(data);
        // 达到BATCH_COUNT了，需要去存储一次数据库，防止数据几万条数据在内存，容易OOM
        if (list.size() >= BATCH_COUNT) {
            saveData();
            // 存储完成清理 list
            list.clear();
        }
    }

    /**
     * 所有数据解析完成了 都会来调用
     *
     * @param context
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        // 这里也要保存数据，确保最后遗留的数据也存储到数据库
        saveData();
        log.info("所有数据解析完成！");
    }
    /**
     * 加上存储数据库
     */
    private void saveData() {
        log.info("{}条数据，开始存储数据库！", list.size());
        service.saveBatch(list);
        if (list.size()>0){
            if ( list.get(0) instanceof Apeo ){
                apeoService.saveUserBatch((List<Apeo>) list);
            }
        }

        log.info("存储数据库成功！");
    }
}